package com.example.ShoppingManagementSystem.service;

import com.example.ShoppingManagementSystem.entity.Admin;
import com.example.ShoppingManagementSystem.entity.Customer;
import com.example.ShoppingManagementSystem.entity.Product;
import com.example.ShoppingManagementSystem.repository.AdminRepository;
import com.example.ShoppingManagementSystem.repository.CustomerRepository;
import com.example.ShoppingManagementSystem.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServicempl implements AdminService {
    private final AdminRepository adminRepository;
    @Autowired
    public AdminServicempl(AdminRepository adminRepository, ProductRepository productRepository, CustomerRepository customerRepository) {
        this.adminRepository = adminRepository;
        this.productRepository = productRepository;

        this.customerRepository = customerRepository;
    }

    private final ProductRepository productRepository;



    private final CustomerRepository customerRepository;

    @Override
    public Admin loginAdmin(String username, String password) {
        if (adminRepository.findByUsername(username)==null) {
            //你这里异常了   你需要自定义全局捕获异常  返回给前端
            throw new RuntimeException("Admin not found");
        }
        Admin user = adminRepository.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        throw new RuntimeException("The password is incorrect");
    }

    @Override
    public void updatePassword( String newPassword) {
        if (adminRepository.findById(1).isEmpty()) {
            throw new RuntimeException("Admin not found");
        }
        Admin admin=adminRepository.getReferenceById(1);

        adminRepository.save(admin);
    }

    @Override
    public void changeCustomerPassword(String username, String newPassword) {
        if (customerRepository.findByUsername(username)==null) {
            throw new RuntimeException("User does not exists.");
        }
        Customer customer=customerRepository.findByUsername(username);

        customerRepository.save(customer);
    }

    @Override
    public List<Customer> getAllCustomers() {return customerRepository.findAll();}

    @Override
    public void delCustomer(int id) {
        if (customerRepository.findById(id).isEmpty()) {
            throw new RuntimeException("User does not exists.");
        }
        customerRepository.deleteById(id);
    }

    @Override
    public Customer findCustomerByUsername(String username) {
        if (customerRepository.findByUsername(username)==null) {
            throw new RuntimeException("User does not exists.");
        }
        return customerRepository.findByUsername(username);
    }

    @Override
    public Customer findCustomerByID(int id) {
        if (customerRepository.findById(id).isEmpty()) {
            throw new RuntimeException("User does not exists.");
        }
        return customerRepository.getReferenceById(id);
    }

    @Override
    public List<Product> getAllProduct() {
        return productRepository.findAll();
    }

    @Override
    public void addProduct(String ProductName, String manufacturer, String model, Double purchasePrice, Double retailPrice, Integer quantity) {
        if(productRepository.findByName(ProductName)!=null) {
            throw new RuntimeException("Product already exists.");
        }
        Product product=new Product(ProductName,manufacturer,model,purchasePrice,retailPrice,quantity);
        productRepository.save(product);
    }

    @Override
    public void updateProduct(Product product) {
        if (productRepository.findById(product.getId()).isEmpty()) {
            throw new RuntimeException("Product does not exists.");
        }
        productRepository.save(product);
    }

    @Override
    public void deleteProduct(int productId) {
        if (productRepository.findById(productId).isEmpty()) {
            throw new RuntimeException("Product does not exists.");
        }
        productRepository.deleteById(productId);
    }

}
